package dda;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

public class ListJoiner implements Observer {

	private int lastThreadNumber = 0;
	private Comparable[][] lists = new Comparable[3][];
	private Comparable[] list;
	int counter = 0;

	public ListJoiner() {

	}

	public ListJoiner getListJoiner() {
		return this;
	}

	public int getNextThreadNumber() {
		return 0;
	}

	public void join() {
		if(lastThreadNumber == lists.length){
			int listSize = 0;
			for(int i = 0; i < lists.length;i++){
				listSize += lists[i].length;
			}
			list  = new Comparable[listSize];
			for(int i = 0; i < lists.length; i++){
				System.arraycopy(lists[i], 0, list, counter, lists[i].length);
				counter+= lists[i].length;
			}
			printJoinedList();
		}
		
			// System.out.println("listParts["+ lastThreadNumber +"]["+ i +
			// "] = " + listParts[lastThreadNumber][i]);
			// System.out.println("Length of listParts[1] = " +
			// listParts[1].length);
	
		

	}

	public void printJoinedList() {
		for(int i = 0 ; i < list.length;i++){
			System.out.println(list[i]);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		int threadNumber = ((SorterThread) o).getThreadNumber();
		Comparable[] c = (Comparable[])arg;
		
		lists[threadNumber] = new Comparable[c.length];
		lists[threadNumber] = c;
		this.lastThreadNumber++;
		join();
	}
}
